package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.api.VenueReportDrawingHistoryFeign;
import com.eastfair.home.dto.VenueReportDrawingHistoryDTO;
import com.eastfair.venueservice.dto.VenueDrawingCategoryAuditDTO;
import com.eastfair.venueservice.service.VenueManageVenueReportDrawingHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主场 报馆提交历史 feign.
 *
 * @author dqq
 * @date 2022 /11/7
 */
@Service
@Slf4j
public class VenueManageVenueReportDrawingHistoryServiceImpl implements VenueManageVenueReportDrawingHistoryService {

    @Resource
    private VenueReportDrawingHistoryFeign venueReportDrawingHistoryFeign;

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public boolean updateBatchAuditStatus(List<VenueReportDrawingHistoryDTO> list) {
        log.info("updateBatchAuditStatus - 批量更新提交历史的审核状态, list={}", list);
        R<Boolean> result = venueReportDrawingHistoryFeign.updateBatchAuditStatus(list);
        log.info("updateBatchAuditStatus - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateBatchByDrawingAuditList(List<VenueDrawingCategoryAuditDTO> list) {
        // 更新主场提交历史的审核记录
        List<VenueReportDrawingHistoryDTO> historyList = list
                .stream()
                .map(venueDrawingCategoryAuditDTO -> BeanUtil.toBean(venueDrawingCategoryAuditDTO, VenueReportDrawingHistoryDTO.class))
                .collect(Collectors.toList());
        return updateBatchAuditStatus(historyList);
    }

    @Override
    public void asyncUpdateBatchByDrawingAuditList(List<VenueDrawingCategoryAuditDTO> list) {
        threadPoolTaskExecutor.execute(() -> updateBatchByDrawingAuditList(list));
    }
}